<template>
	<view>
		<view class="box1">
			<view :style="bj" class="buttonBox1" @click="choose_image">
				预览存储
			</view>

			<view :style="bj" class="buttonBox1" @click="upload_image">
				批量上传
			</view>
		</view>

		<view class="box2">

			<view :style="bj" class="buttonBox1" @click="scan_barcode">
				扫码
			</view>
			<view :style="bj" class="buttonBox1" @click="scan_barcode_long">
				连续扫码
			</view>
		</view>


	</view>

</template>
<script>
	import maskModel from './../../components/mask-model/mask-model.vue'

	export default {
		components: {

			maskModel
		},
		data() {
			return {

				headTitleId: true,
				title: "",
				fontcolor: {
					color: this.$kdup.color,
					borderBottom: '2px solid ' + this.$kdup.color
				},
				bj: {
					backgroundColor: this.$kdup.color,
				},
				color: this.$kdup.color


			}
		},

		//表体初始化  
		onLoad(e) {
			this.eobject = e;
			uni.setNavigationBarColor({
				frontColor: "#ffffff", //文字颜色
				backgroundColor: this.$kdup.color //底部背景色
			})
			this.init(e);



		},
		//当表体只有一行的时候会报错
		methods: {

			init: function(e) {
				uni.setNavigationBarTitle({
					title: '测试DEMO'
				});

			},
			scan_barcode: function() {
				uni.scanCode({
					success: (res) => {
						uni.showToast({
							title: "扫码结果：" + res.result,
							icon: "none",
							duration: 6000
						});
					}
				});
			},
			scan_barcode_long: function() {
				uni.navigateTo({
					url: '/pages/scancode/scancode_long'
				});


			},
			choose_image: function() {

				// 从相册选择6张图
				uni.chooseImage({
					count: 6,
					sizeType: ['original'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						// 预览图片
						uni.previewImage({
							urls: res.tempFilePaths,
							longPressActions: {
								itemList: ['保存','上传'],
								success: function(data) {
									var temp_arr = res.tempFilePaths;
									if (data.tapIndex == 0) {
											//保存到本地相册
											uni.saveImageToPhotosAlbum({
												filePath: temp_arr[data.index],
												success: function(r) {
													uni.showToast({
														title: '保存成功',
														icon: "none",
														duration: 1000
													});
												}
											});
										
									}
									if (data.tapIndex ==1) {
											uni.uploadFile({
												url: uni.getStorageSync("kdup_url")+ "/app/upload", //仅为示例，非真实的接口地址
												filePath: temp_arr[data.index],
												name: 'file',
												formData: {
													'user':uni.getStorageSync("operateror") 
												},
												success: (uploadFileRes) => {
													uni.showToast({
														title: uploadFileRes.data,
														icon: "none",
														duration: 2000
													});
													
												}
											});
									
											// uploadTask.onProgressUpdate((res) => {
											// 	uni.showToast({
											// 		title: '上传进度' + res.progress+'已经上传的数据长度' + res.totalBytesSent+'预期需要上传的数据总长度' + res.totalBytesExpectedToSend,
											// 		icon: "none",
											// 		duration: 6000
											// 	});
											
									
												// 测试条件，取消上传任务。
												// if (res.progress > 50) {
												// 	uploadTask.abort();
												// }
											//});
									}


								},
								fail: function(data) {
									uni.showToast({
										title: data.errMsg,
										icon: "none",
										duration: 1000
									});

								}
							}
						});
					}
				});

			},
			upload_image:function()
			{
				//最多限制100张
				uni.chooseImage({
					count: 100,
					sizeType: ['original'],
					sourceType: ['album'],
					success: function(res) {
						var arr=res.tempFiles;
						for(var i=0;i<arr.length;i++)
						{
							uni.uploadFile({
								url: uni.getStorageSync("kdup_url")+ "/app/upload", 
								filePath: arr[i].path,
								name: 'file',
								formData: {
									'user':uni.getStorageSync("operateror"),'pages':i+1
								},
								success: (uploadFileRes) => {

								}
							});
						}
						uni.showToast({
							title: 'success',
							icon: "none",
							duration: 2000
						});
									
											// uploadTask.onProgressUpdate((res) => {
											// 	uni.showToast({
											// 		title: '上传进度' + res.progress+'已经上传的数据长度' + res.totalBytesSent+'预期需要上传的数据总长度' + res.totalBytesExpectedToSend,
											// 		icon: "none",
											// 		duration: 6000
											// 	});
											
									
												// 测试条件，取消上传任务。
												// if (res.progress > 50) {
												// 	uploadTask.abort();
												// }
											//});
									
				
				
								}
					});
				},
				
			
		

	

		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}

	.box1 {
		width: 100%;
		height: 84upx;
		display: flex;
		justify-content: center;
		flex-direction: row;
		position: fixed;
		top: 120upx;
		left: 0;
		right: 0;
		z-index: 999;


	}

	.box2 {
		width: 100%;
		height: 84upx;
		display: flex;
		justify-content: center;
		flex-direction: row;
		position: fixed;
		top: 210upx;
		left: 0;
		right: 0;
		z-index: 999;


	}

	.box3 {
		width: 100%;
		height: 84upx;
		display: flex;
		justify-content: center;
		flex-direction: row;
		position: fixed;
		top: 300upx;
		left: 0;
		right: 0;
		z-index: 999;


	}

	.buttonBox1 {
		margin-right: 2rpx;
		width: 50%;
		height: 84upx;
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>
